package com.shujia;

import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Properties;

public class Code7InsertData {
    static Connection connection;
    static {
        Properties properties = new Properties();
        try {
            properties.load(new FileReader("source/jdbc.properties"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            Class.forName(properties.getProperty("driver"));
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        try {
            connection = DriverManager.getConnection(
                    properties.getProperty("url"),
                    properties.getProperty("user"),
                    properties.getProperty("password")
            );
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    // 插入多条数据
    public static void main(String[] args) throws SQLException {
        // 获取当前系统时间戳
        long start = System.currentTimeMillis();
        String insertSql = "INSERT INTO useinfo values(?,?,?,?)";
        PreparedStatement preparedStatement = connection.prepareStatement(insertSql);
        for (int i = 0; i < 10000 * 10; i++) {
            preparedStatement.setString(1,Integer.toString(i));
            preparedStatement.setString(2,"zhangsan");
            preparedStatement.setString(3,"zhangsan@163.com");
            preparedStatement.setString(4,"18");
            preparedStatement.executeUpdate();
            System.out.println("一条数据插入成功...");
        }
        // 获取当前系统时间戳
        long end = System.currentTimeMillis();
        preparedStatement.close();
        connection.close();
        System.out.println("执行插入时间为："+(end-start)/1000+"秒");
    }
}
